package org.libermundi.theorcs.chronicles.tapestry.pages.message;

import java.util.List;

import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.beaneditor.BeanModel;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.BeanModelSource;
import org.apache.tapestry5.services.PropertyConduitSource;
import org.libermundi.theorcs.chronicles.dto.ChronicleSettings;
import org.libermundi.theorcs.chronicles.model.Persona;
import org.libermundi.theorcs.chronicles.model.message.impl.InternalEmail;
import org.libermundi.theorcs.chronicles.services.message.MessageChainService;
import org.libermundi.theorcs.chronicles.tapestry.base.ChroniclePage;

/**
 * Main page for the message management. 
 * Displays the list of messages and a message when selected by the user
 * 
 */
public class Box extends ChroniclePage {

	public static final String PAGE_ID = "chronicles.pages.mail.index";

	/* (non-Javadoc)
	 * @see org.libermundi.theorcs.core.tapestry.BasePage#getPageId()
	 */
	@Override
	public String getPageId() {
		return PAGE_ID;
	}

	/**
	 * get the settings of the chronicle loaded for the session by the user
	 */
	@SessionState
	private ChronicleSettings _chronicleSettings;

	@Inject
	private MessageChainService<InternalEmail, Persona> _messageManager;

	@Property
	private List<InternalEmail> _messageItems;

	@Property
	private InternalEmail _messageItem;

	/**
	 * BeanModel of InternalEmail to display the complex class sender.name
	 */
	@Property
	private BeanModel<InternalEmail> _internalEmailModel;

	@Inject
	private BeanModelSource _beanModelSource;

	@Inject
	private Messages _messages;

	@Inject
	private PropertyConduitSource _pcSource;

//	/**
//	 * MessageId of the selected message displayed
//	 */
//	private long _messageId;
//
//	/**
//	 * Zone for ajax reloading displaying the message selected
//	 */
//	@InjectComponent
//	private Zone _viewMessage;
//	
	@SetupRender
	public void setupRender(){
		// load the messages of the persona
		_messageItems = _messageManager.getEntityMessages(_chronicleSettings.getPersona());
		// have to add the property sender to display it as it is a complex class
		_internalEmailModel = _beanModelSource.createDisplayModel(
				InternalEmail.class, _messages);
		_internalEmailModel.add("sender", _pcSource.create(InternalEmail.class, "sender.persona.name")
				).sortable(true);
		_internalEmailModel.exclude("id", "body", "seen");
		_internalEmailModel.reorder("sender","title","sendingdate");		
	}

//	/**
//	 * is called by Tapestry to pass in the activation context from the URL.
//	 * 
//	 * @param messageId
//	 */
//	Object onActionFromSelect(long messageId) {
//		// load the messages of the persona
//		_messageItems = _messageManager.getEntityMessages(_chronicleSettings.getPersona());
//		// get the message corresponding to the id
//		if (_messageItems.size() != 0) {
//			InternalEmail tmp = null;
//			ListIterator<InternalEmail> ite = _messageItems.listIterator();
//			while (ite.hasNext()) {
//				tmp = ite.next();
//				if (tmp.getId() == messageId)
//					break;
//			}
//			_messageItem = tmp;
//			this._messageId = messageId;
//		}
//		return _viewMessage.getBody();
//	}

//	/**
//	 * is called by Tapestry to get the activation context to put in the URL.
//	 * 
//	 * @return messageId
//	 */
//	long onPassivate() {
//		return this._messageId;
//	}

}
